﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Transactions;

public partial class Controls_CodeMapMgnt : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gv.DataBind();
        }
    }

    protected void gv_DataBound(object sender, EventArgs e)
    {
        //gv.Columns[0].HeaderText = 
    }

    protected void gv_ItemCommand(object sender, GridViewCommandEventArgs e)
    {
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        var db = new DB();

        using (TransactionScope scope = new TransactionScope())
        {

            db.ExecuteNonQuery(string.Format(@"INSERT CodeMap (TableName,ColName,Value,{0},Status,CreateDate,CreateBy)
                    SELECT @TableName,@ColName,@Value,@Text,'Y',getdate(), @CreateBy ", LocManager.Current.GetTextCol("CodeMap", "Text"))
                , new DBParameter("TableName", TableName)
                , new DBParameter("ColName", ColumnName)
                , new DBParameter("Value", txtValue.Text)
                , new DBParameter("Text", txtText.Text)
                , new DBParameter("CreateBy", EmpManager.Current.EmpID));


            scope.Complete();
        }

        plQuery.DataBind();
        gv.DataBind();
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        gv.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (SaveData() == true)
        {
            MyObj.ShowMessage("Form", "msgSaveOK");
        }
    }

    protected bool SaveData()
    {
        var db = new DB();

        using (TransactionScope scope = new TransactionScope())
        {
            foreach (GridViewRow row in gv.Rows)
            {
                db.ExecuteNonQuery(string.Format(@"UPDATE CodeMap SET {0}=@Text, UpdateBy=@UpdateBy, UpdateDate=getdate()
                    WHERE TableName=@TableName AND ColName=@ColName AND Value=@Value ", LocManager.Current.GetTextCol("CodeMap", "Text"))
                    , new DBParameter("TableName", TableName)
                    , new DBParameter("ColName", ColumnName)
                    , new DBParameter("Value", gv.DataKeys[row.RowIndex]["Value"])
                    , new DBParameter("Text", (row.FindControl("txtText") as ITextControl).Text)
                    , new DBParameter("UpdateBy", EmpManager.Current.EmpID));
            }

            scope.Complete();
        }

        return true;
    }

    public string TableName
    {
        get
        {
            return ViewState["TableName"] as string;
        }
        set
        {
            Logger.WriteLog(value);
            ViewState["TableName"] = value;
        }
    }

    public string ColumnName
    {
        get
        {
            return ViewState["ColumnName"] as string;
        }
        set
        {
            ViewState["ColumnName"] = value;
        }
    }
}